using MicroMsg.Common.Event;
using MicroMsg.Network;
using MicroMsg.Plugin;
using MicroMsg.Resource.@string;
using MicroMsg.Scene;
using MicroMsg.UI.UserContrl;
using MicroMsg.UI.Util;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Imaging;
namespace MicroMsg.Source.UI.QQFriends
{
	public class BindQQ : BasePage
	{
		private const string TAG = "BindQQView";
		private EventWatcher m_WatcherQQBindErr;
		private EventWatcher m_WatcherQQBindSucess;
		private EventWatcher m_WatcherQQBindNeedVCode;
		private string imgSid;
		private byte[] imgBuf;
		private int QQInputView;
		private int VCodeView = 1;
		private bool sendReq;
		private object[] nameObj;
		private ApplicationBar mBar;
		internal Grid LayoutRoot;
		internal panelTitle2Line panelTitle;
		internal StackPanel ContentPanel;
		internal Grid InputView;
		internal TextBlock TitleName;
		internal TextBlock textBlockAccount;
		internal PhoneTextBox textBoxAccount;
		internal TextBlock textBlockPassword;
		internal WaterMarkPassWordBox textBoxPassword;
		internal Grid VerifyView;
		internal TextBlock TitleNameTxt;
		internal Image VCodeImage;
		internal PhoneTextBox textBoxVCode;
		internal TextBlock textBlock2;
		private bool _contentLoaded;
		public BindQQ()
		{
			this.InitializeComponent();
			base.add_Loaded(new RoutedEventHandler(this.BindQQ_Loaded));
		}
		private void BindQQ_Loaded(object sender, RoutedEventArgs e)
		{
			this.setAppBar();
		}
		protected override void OnPageIn(object[] object_0)
		{
			this.m_WatcherQQBindErr = new EventWatcher(this, null, new EventHandlerDelegate(this.onEventHandlerQQAccountErr));
			this.m_WatcherQQBindSucess = new EventWatcher(this, null, new EventHandlerDelegate(this.onEventHandlerQQBindSucess));
			this.m_WatcherQQBindNeedVCode = new EventWatcher(this, null, new EventHandlerDelegate(this.onEventHandlerQQBindNeedVCode));
			EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_BINDQQ_ERR, this.m_WatcherQQBindErr);
			EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_BINDQQ_SUCCESS, this.m_WatcherQQBindSucess);
			EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_BINDQQ_NEEDVERIFY, this.m_WatcherQQBindNeedVCode);
			this.ShowView(this.QQInputView);
			this.sendReq = false;
			if (object_0 != null)
			{
				this.nameObj = object_0;
				string text = object_0[0] as string;
				if (text == "setting")
				{
					this.panelTitle.strTitle = strings.COM_Setting;
				}
			}
		}
		protected override void OnPageOut()
		{
			if (this.m_WatcherQQBindErr == null)
			{
				return;
			}
			EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_BINDQQ_ERR, this.m_WatcherQQBindErr);
			this.m_WatcherQQBindErr = null;
			EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_BINDQQ_SUCCESS, this.m_WatcherQQBindSucess);
			this.m_WatcherQQBindSucess = null;
			EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_BINDQQ_NEEDVERIFY, this.m_WatcherQQBindNeedVCode);
			this.m_WatcherQQBindNeedVCode = null;
		}
		private void OnVCodeImageTap(object sender, RoutedEventArgs e)
		{
			ServiceCenter.sceneBindQQ.doSceneRefetchVerifyImgForBindQQ();
			this.sendReq = false;
		}
		private void DoBindQQ()
		{
			if (this.textBoxAccount.get_Text() == "")
			{
				MessageBoxManager.ShowToast(strings.BindQQ_InputQQID, 2500, null, false);
				return;
			}
			if (this.textBoxPassword.Password == "")
			{
				MessageBoxManager.ShowToast(strings.BindQQ_InputQQPWD, 2500, null, false);
				return;
			}
			try
			{
				uint bindUin = Convert.ToUInt32(this.textBoxAccount.get_Text());
				ServiceCenter.sceneBindQQ.doSceneBindQQ(bindUin, this.textBoxPassword.Password);
				this.showProgressCover(strings.BindQQ_Binding);
			}
			catch (OverflowException)
			{
				this.ShowView(this.QQInputView);
				MMMessageBox.Show(strings.BindQQ_WrongQQ, true, "确定");
			}
			catch (FormatException)
			{
				this.ShowView(this.QQInputView);
				MMMessageBox.Show(strings.BindQQ_WrongQQ, true, "确定");
			}
		}
		private void DoVerifyCode()
		{
			ServiceCenter.sceneBindQQ.doSceneBindQQWithVerify(this.imgSid, this.textBoxVCode.get_Text());
			this.showProgressCover(strings.BindQQ_NowVerifying);
			this.sendReq = true;
		}
		private void onEventHandlerQQAccountErr(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			this.sendReq = false;
			int num = (int)evtArgs.mObject;
			if (num == -12)
			{
				this.ShowView(this.QQInputView);
				MMMessageBox.Show(strings.BindQQ_QQBinded, true, "确定");
				return;
			}
			if (num == -3)
			{
				this.ShowView(this.QQInputView);
				MMMessageBox.Show(strings.BindQQ_WrongPwd, true, "确定");
				return;
			}
			if (num == -4)
			{
				this.ShowView(this.QQInputView);
				MMMessageBox.Show(strings.BindQQ_NoUser, true, "确定");
				return;
			}
			if (num == -33)
			{
				this.ShowView(this.VCodeView);
				MMMessageBox.Show(strings.BindQQ_GetVerifyFail, true, "确定");
				return;
			}
			if (num == -32)
			{
				this.ShowView(this.VCodeView);
				MMMessageBox.Show(strings.BindQQ_VerifyFailTip, true, "确定");
				return;
			}
			this.ShowView(this.QQInputView);
			MMMessageBox.Show(strings.BindQQ_Fail, true, "确定");
		}
		private void onEventHandlerQQBindSucess(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			this.ShowView(this.QQInputView);
			PluginLoader.preLoadPlugin("ViewQQInfo");
			PluginLoader.preLoadPlugin("QQOfflineMsg");
			base.GoToPage("/Source/UI/Setting/BindedQQPage.xaml", this.nameObj, false);
		}
		private void onEventHandlerQQBindNeedVCode(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			this.ShowView(this.VCodeView);
			if (this.sendReq)
			{
				MessageBoxManager.ShowToast(strings.BindQQ_VerifyFail, 2500, null, false);
			}
			if (!evtArgs.checkType<VerifyCodeArgs>())
			{
				return;
			}
			VerifyCodeArgs verifyCodeArgs = evtArgs.mObject as VerifyCodeArgs;
			this.imgSid = verifyCodeArgs.mImageSid;
			this.imgBuf = verifyCodeArgs.mImageBuf;
			BitmapImage bitmapImage = new BitmapImage();
			MemoryStream source = new MemoryStream(this.imgBuf);
			try
			{
				bitmapImage.SetSource(source);
			}
			catch (Exception)
			{
			}
			this.VCodeImage.set_Source(bitmapImage);
		}
		private ApplicationBar setAppBar()
		{
			if (this.mBar != null)
			{
				if (!this.mBar.get_IsVisible())
				{
					this.mBar.set_IsVisible(true);
				}
				return this.mBar;
			}
			this.mBar = new ApplicationBar();
			this.mBar.set_IsMenuEnabled(true);
			this.mBar.set_IsVisible(true);
			ApplicationBarIconButton applicationBarIconButton = new ApplicationBarIconButton(new Uri("/icons/appbar.check.rest.png", 2));
			applicationBarIconButton.set_Text(strings.COM_Confirm);
			applicationBarIconButton.add_Click(new EventHandler(this.btnOK_Click));
			this.mBar.get_Buttons().Add(applicationBarIconButton);
			base.set_ApplicationBar(this.mBar);
			this.setAppBarStyle(base.get_ApplicationBar());
			return this.mBar;
		}
		private void btnOK_Click(object sender, EventArgs e)
		{
			base.Focus();
			if (this.VerifyView.get_Visibility() == 1)
			{
				this.DoBindQQ();
				return;
			}
			this.DoVerifyCode();
		}
		private void ShowView(int viewIndex)
		{
			if (viewIndex == this.QQInputView)
			{
				this.VerifyView.set_Visibility(1);
				this.InputView.set_Visibility(0);
				this.hideProgressCover();
				return;
			}
			this.VerifyView.set_Visibility(0);
			this.InputView.set_Visibility(1);
			this.hideProgressCover();
		}
		private void showProgressCover(string strCoverTitle)
		{
			ProcessingCover.getProgressBar(this.LayoutRoot, strCoverTitle, null, false, false, null);
		}
		private void hideProgressCover()
		{
			ProcessingCover.releaseProgressBar(this.LayoutRoot);
		}
		private void inputTxt_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.get_Key() == 3)
			{
				if (sender == this.textBoxPassword)
				{
					base.Focus();
					this.DoBindQQ();
					return;
				}
				if (sender == this.textBoxVCode)
				{
					base.Focus();
					this.DoVerifyCode();
				}
			}
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/UI/QQFriends/BindQQ.xaml", 2));
			this.LayoutRoot = (Grid)base.FindName("LayoutRoot");
			this.panelTitle = (panelTitle2Line)base.FindName("panelTitle");
			this.ContentPanel = (StackPanel)base.FindName("ContentPanel");
			this.InputView = (Grid)base.FindName("InputView");
			this.TitleName = (TextBlock)base.FindName("TitleName");
			this.textBlockAccount = (TextBlock)base.FindName("textBlockAccount");
			this.textBoxAccount = (PhoneTextBox)base.FindName("textBoxAccount");
			this.textBlockPassword = (TextBlock)base.FindName("textBlockPassword");
			this.textBoxPassword = (WaterMarkPassWordBox)base.FindName("textBoxPassword");
			this.VerifyView = (Grid)base.FindName("VerifyView");
			this.TitleNameTxt = (TextBlock)base.FindName("TitleNameTxt");
			this.VCodeImage = (Image)base.FindName("VCodeImage");
			this.textBoxVCode = (PhoneTextBox)base.FindName("textBoxVCode");
			this.textBlock2 = (TextBlock)base.FindName("textBlock2");
		}
	}
}
